<?php

class LangEngineMysql extends DataObject implements LangEngineInterface 
{
    public $_dbTable = 'lang_words';
	public $_textsTable = 'lang_texts';
	public $_wordsTable = 'lang_words';
    public $_dbIndex = 'indexID';
    public $_textsIndex = 'indexID';
    public $_wordsIndex = 'indexID';
    
    public $_arFields = array('lang',
                              'translation');

    public $debug = false;
    private $cache = array();

    public function getWord($indexID, $lang)
    {
		if(isset($cache[$indexID])) return $cache[$indexID];
        else
        {
            if ($this->get(array("AND"=>array($this->_dbIndex." ='". $this->txt2sql($indexID) ."'", "lang='". $this->txt2sql($lang) ."'")))) 
            {
                return $this->translation;
            } 
            else if($debug) 
            {
                $this->setID($indexID);
                $this->save();
                $this->set(array("lang"=>$lang, "translation"=>$indexID));
            }
        }
    }
    
    public function setWord($indexID, $lang, $translation)
    {
		$this->set(array($this->_dbIndex=>$indexID,
                         "lang"=>$lang,
                         "translation"=>$translation));
        $this->save();
    }
    
    public function changeType($type) {
        switch($type) {
            case 'text':$this->_dbTable = $this->_textsTable; 
                        $this->_dbIndex = $this->_textsIndex;  
                        break;
            case 'word':$this->_dbTable = $this->_wordsTable;
                        $this->_dbIndex = $this->_wordsIndex;
                        break;
        }
    }
}

?>
